
package com.genesis.yunnanji.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.genesis.yunnanji.R;
import com.genesis.yunnanji.activity.StoreWeb;
import com.genesis.yunnanji.bean.HomePageListBean;
import com.genesis.yunnanji.config.GenesisApiConfig;
import com.genesis.yunnanji.utils.GenesisUtils;

import java.util.List;

public class HorizontalPagerAdapter extends PagerAdapter {

    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private List<HomePageListBean.ResultBean.ListBean.AdvertListBean> mData;
    private boolean mIsTwoWay;
    private GenesisUtils genesisUtils = GenesisUtils.getIntance();

    public HorizontalPagerAdapter(final Context context, List<HomePageListBean.ResultBean.ListBean.AdvertListBean> mData) {
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
        this.mData = mData;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public int getItemPosition(final Object object) {
        return POSITION_NONE;
    }

    @Override
    public Object instantiateItem(final ViewGroup container, final int position) {
        final View view;

        view = mLayoutInflater.inflate(R.layout.item_coverflow, container, false);
        ImageView imageView = (ImageView) view.findViewById(R.id.img_item);
        Glide.with(mContext).load(GenesisApiConfig.PIC_HOST + mData.get(position).getAdvert_img()).into(imageView);
        container.addView(view);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, StoreWeb.class);
                if (genesisUtils.isLogin()) {
                    intent.putExtra("url", mData.get(position).getTarget_link() + GenesisApiConfig.FROM_APP + GenesisApiConfig.APP_TOKEN + genesisUtils.getAppToken());
                } else {
                    intent.putExtra("url", mData.get(position).getTarget_link() + GenesisApiConfig.FROM_APP);
                }

                mContext.startActivity(intent);
            }
        });
        return view;
    }

    @Override
    public boolean isViewFromObject(final View view, final Object object) {
        return view.equals(object);
    }

    @Override
    public void destroyItem(final ViewGroup container, final int position, final Object object) {
        container.removeView((View) object);
    }
}